package com.gxuwz.bignews.view.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.gxuwz.bignews.R;
import com.bumptech.glide.Glide;
import android.app.AlertDialog;
import com.gxuwz.bignews.model.db.UserDao;
import android.widget.Toast;
import com.google.android.material.button.MaterialButton;

public class UserSettingActivity extends AppCompatActivity {
    private ImageView ivBack, ivAvatar;
    private EditText etUsername;
    private TextView tvTitle, tvChangeAvatar, tvPhone;
    private MaterialButton btnSave;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_setting);

        ivBack = findViewById(R.id.iv_back);
        ivAvatar = findViewById(R.id.iv_avatar);
        etUsername = findViewById(R.id.et_username);
        tvTitle = findViewById(R.id.tv_title);
        tvChangeAvatar = findViewById(R.id.tv_change_avatar);
        tvPhone = findViewById(R.id.tv_phone);
        btnSave = findViewById(R.id.btn_save);

        // 返回键点击关闭页面
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 读取用户信息（以 SharedPreferences 为例）
        SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
        String username = sp.getString("username", "");
        String phone = sp.getString("phone", "");
        String avatar = sp.getString("avatar", "cat"); // 默认头像

        etUsername.setText(username);
        tvPhone.setText(phone);

        // 设置头像（可根据实际头像类型选择资源）
        int avatarRes = R.drawable.ic_avatar_cat;
        if ("dog".equals(avatar)) avatarRes = R.drawable.ic_avatar_dog;
        else if ("man".equals(avatar)) avatarRes = R.drawable.ic_avatar_male;
        else if ("female".equals(avatar)) avatarRes = R.drawable.ic_avatar_female;
        else if ("chicken".equals(avatar)) avatarRes = R.drawable.ic_avatar_chicken;
        Glide.with(this).load(avatarRes).into(ivAvatar);

        // 头像选择逻辑
        tvChangeAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] avatarNames = {"dog", "cat", "man", "female", "chicken"};
                final int[] avatarResIds = {R.drawable.ic_avatar_dog, R.drawable.ic_avatar_cat, R.drawable.ic_avatar_male, R.drawable.ic_avatar_female, R.drawable.ic_avatar_chicken};
                // 获取当前头像
                SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
                String currentAvatar = sp.getString("avatar", "cat");
                int currentIndex = 1;
                for (int i = 0; i < avatarNames.length; i++) {
                    if (avatarNames[i].equals(currentAvatar)) {
                        currentIndex = i;
                        break;
                    }
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(UserSettingActivity.this);
                builder.setTitle("选择头像");
                builder.setSingleChoiceItems(new String[]{"狗狗", "猫咪", "男生", "女生", "小鸡"}, currentIndex, (dialog, which) -> {
                    // 更新UI
                    Glide.with(UserSettingActivity.this).load(avatarResIds[which]).into(ivAvatar);
                    // 更新数据库
                    String phoneKey = sp.getString("phone", "");
                    new UserDao(UserSettingActivity.this).updateAvatar(phoneKey, avatarNames[which]);
                    // 更新本地缓存
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("avatar", avatarNames[which]);
                    editor.apply();
                    dialog.dismiss();
                });
                builder.setNegativeButton("取消", null);
                builder.show();
            }
        });

        // 保存按钮逻辑
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newUsername = etUsername.getText().toString().trim();
                if (newUsername.isEmpty()) {
                    Toast.makeText(UserSettingActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
                String phone = sp.getString("phone", "");
                // 更新数据库
                new UserDao(UserSettingActivity.this).updateUsername(phone, newUsername);
                // 更新本地缓存
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("username", newUsername);
                editor.apply();
                Toast.makeText(UserSettingActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
} 